package com.zuoshenge.sys.service;

import com.zuoshenge.sys.bean.annotation.RedissonLock;
import com.zuoshenge.sys.dao.UserDao;
import com.zuoshenge.sys.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class GameService {

    @Resource
    private UserDao userDao;

    // TODO 使用 一锁二判三更新 实现
    public void reduceFree(String username, Integer points) {
        User user = userDao.loadUserByUserName(username);
        int newCount = user.getFreeCount() - points;
        if (newCount < 0) {
            throw new RuntimeException("有问题");
        }
        user.setFreeCount(newCount);
        userDao.update(user);
    }

    // TODO 使用 一锁二判三更新 实现
    public void reduceExtra(String username, Integer points) {
        User user = userDao.loadUserByUserName(username);
        int newCount = user.getExtraCount() - points;
        if (newCount < 0) {
            throw new RuntimeException("有问题");
        }
        user.setExtraCount(newCount);
        userDao.update(user);
    }

}
